package com.last.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.last.mapper.HouseMapper;
import com.last.service.IHouseService;
import com.last.entity.HouseName;
import com.last.entity.PageDTO;
import com.last.entity.PageResult;
import com.last.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Service
public class HouseService extends ServiceImpl<HouseMapper,HouseName> implements IHouseService {

    @Autowired
    private HouseMapper houseMapper;

    @Override
    public List<HouseName> findAll() {
        return houseMapper.selectList(null);
    }

    @Override
    public PageResult<HouseName> findAllByPage(PageDTO<HouseName> pageDTO) {


        IPage<HouseName> iPage = new Page<>(pageDTO.getPage(),pageDTO.getLimit());

        HouseName whereEntity = pageDTO.getData();
        QueryWrapper<HouseName> queryWrapper = new QueryWrapper<>();

        //民宿名判断
        if(!StringUtils.isEmpty(whereEntity.getHomeName())){
            queryWrapper.like("home_name",whereEntity.getHomeName());
        }
        //用户名判断（待用ID）
//        if(!StringUtils.isEmpty(whereEntity.getHomeName())){
//            queryWrapper.like("home_name",whereEntity.getHomeName());
//        }
        //用户id判断
        if(!StringUtils.isEmpty(whereEntity.getMerchantId())){
            queryWrapper.eq("merchant_id",whereEntity.getMerchantId());
        }


        IPage<HouseName> pageInfo = houseMapper.selectPage(iPage,queryWrapper);

        PageResult<HouseName> pageResult = new PageResult<>();

        if(pageInfo.getRecords() == null){

            pageResult.setSuccess(false);
            pageResult.setMsg("暂无数据");

        }else{
            pageResult.setData(pageInfo.getRecords());
            pageResult.setTotal(pageInfo.getTotal());
            pageResult.setPages(pageInfo.getPages());
            pageResult.setSuccess(true);
        }

        return pageResult;
    }

//    @Override
//    public Result<HouseName> save(HouseName houseName) {
//        int i = houseMapper.insert(houseName);
//        return new Result<HouseName>(){{setSuccess(i==1);setMessage(i==1?"添加成功":"添加失败");}} ;
//    }

    @Override
    public Result<HouseName> update(HouseName houseName) {
        QueryWrapper<HouseName> queryWrapper = new QueryWrapper<>(houseName);
        int i = houseMapper.updateById(houseName);
        return new Result<HouseName>(){{setSuccess(i==1);setMessage(i==1?"更新成功":"更新失败");}} ;
    }


    @Override
    @Transactional
    public Result<HouseName> del(Integer homeId) {

        QueryWrapper<HouseName> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("home_id",homeId);

        //同步删除民宿名下的所有房间
        //Integer i = restTemplate.getForObject("http://127.0.0.1:(房型接口号)+(后续删除窄化请求)",Integer.class);

//        if(i == 1){
//            int j = houseMapper.deleteById(homeId);
//            return new Result<HouseName>(){{setSuccess(j==1);setMsg(j==1?"删除成功":"删除失败");}} ;
//        }
        int j = houseMapper.deleteById(homeId);
        if(j == 1){
            return new Result<HouseName>(){{setSuccess(j==1);setMessage(j==1?"删除成功":"删除失败");}} ;
        }

        return new Result<HouseName>(){{setSuccess(false);setMessage("删除失败");}} ;



    }
}
